/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.order.client  
 */
package com.apache.client;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.apache.api.vo.ResultMsg;
import com.apache.attachment.mommon.ToolsUtil;
import com.apache.info.entity.PubMetadata;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;

/**
 * description:  客户端action父类
 * @author Hou Dayu 创建时间：2015-8-11  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
@SuppressWarnings("serial")
public class InfoReleaseActionSupport extends HttpServlet {

	protected LoginUser getSessionUser(HttpServletRequest request) {
		LoginUser loginUser = (LoginUser) request.getSession().getAttribute("loginUser");
		return loginUser;
	}

	protected Map<String, String> getRequestMap(HttpServletRequest request) {
		return getParameterMap(request);
	}

	protected Map<String, Object> getMethodParams(HttpServletRequest request) {
		Map<String, Object> bodyMap = new HashMap<String, Object>();
		Map<String, String> map = getParameterMap(request);
		bodyMap.putAll(map);
		return bodyMap;
	}

	@SuppressWarnings("rawtypes")
	private Map<String, String> getParameterMap(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		Set keSet = request.getParameterMap().entrySet();
		for (Iterator itr = keSet.iterator(); itr.hasNext();) {
			Map.Entry me = (Map.Entry) itr.next();
			Object ok = me.getKey();
			if ("doCode".equals(ok.toString()))
				continue;
			Object ov = me.getValue();
			String[] value = new String[1];
			if (ov instanceof String[]) {
				value = (String[]) ov;
			} else {
				value[0] = ov.toString();
			}
			if (value.length > 0) {
				map.put(ok.toString(), value[0]);
			}
		}
		String sysPass = ToolsUtil.getInstance().getValueByKey("push_to_dwusoft_pass");
		map.put("sysPass", sysPass);
		return map;
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * 输出json数据
	 */
	protected void outputJson(String json, HttpServletResponse response) throws IOException {
		if (StrUtil.isNotNull(json)) {
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
			out.flush();
			out.close();
		}
	}

	//信息保存
	protected ResultMsg saveInfo(HttpServletRequest request, Map<String, String> params, String sortCode) {
		ResultMsg json = null;
		params.put("sortCode", sortCode);
		List<PubMetadata> list = InfoReleaseCoreCleint.getInstance().getMetadatas(sortCode);
		if (null == list)
			return null;
		for (PubMetadata pm : list) {
			if ("1".equals(pm.getDataRestrict()) && StrUtil.isNull(params.get(pm.getDataAttr()))) {//处理主键
				params.put(pm.getDataAttr(), UUID.randomUUID().toString().replaceAll("-", ""));
			} else if ("6".equals(pm.getDataType()) && "2".equals(pm.getDataRestrict())) {//处理日期类型
				if (StrUtil.isNull(params.get(pm.getDataAttr()))) {
					params.put(pm.getDataAttr(), DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
				}
			} else if ("2".equals(pm.getDataRestrict())) {
				if (StrUtil.isNull(params.get(pm.getDataAttr()))) {
					json = new ResultMsg("F", "表单的必填项不可为空");
					return json;
				}
			}
		}
		json = InfoReleaseCoreCleint.getInstance().saveInfo(params);
		return json;
	}

	//信息修改
	protected ResultMsg editInfo(HttpServletRequest request, Map<String, String> params, String sortCode) {
		ResultMsg json = null;
		params.put("sortCode", sortCode);
		PubMetadata pm = InfoReleaseCoreCleint.getInstance().getMetadataPrimarykey(sortCode);
		if (null != pm) {
			if ("1".equals(pm.getDataRestrict())) {//处理主键
				if (StrUtil.isNull(params.get(pm.getDataAttr()))) {
					json = new ResultMsg("F", "非空参数不可为空");
					return json;
				}
			}
		}
		json = InfoReleaseCoreCleint.getInstance().updateInfo(params);
		return json;
	}

	//信息删除
	protected ResultMsg delInfo(HttpServletRequest request, Map<String, String> params, String sortCode) {
		ResultMsg json = null;
		params.put("sortCode", sortCode);
		PubMetadata pm = InfoReleaseCoreCleint.getInstance().getMetadataPrimarykey(sortCode);
		if (null != pm) {
			if ("1".equals(pm.getDataRestrict())) {//处理主键
				if (StrUtil.isNull(params.get(pm.getDataAttr()))) {
					json = new ResultMsg("F", "非空参数不可为空");
					return json;
				}
			}
		}
		json = InfoReleaseCoreCleint.getInstance().deleteInfo(params);
		return json;
	}
}
